package com.apkcore.bl;

import java.util.HashMap;
import java.util.Map;

public class _930和相同的二元子数组 {

    public static void main(String[] args) {
        int[] nums = {1, 0, 1, 0, 1};
        System.out.println(new _930和相同的二元子数组().numSubarraysWithSum(nums, 2));
    }

    public int numSubarraysWithSum(int[] nums, int goal) {
        int sum = 0;
        Map<Integer, Integer> map = new HashMap<>();
        int result = 0;
        for (int num : nums) {
            map.put(sum, map.getOrDefault(sum, 0) + 1);
            sum += num;
            result += map.getOrDefault(sum - goal, 0);
        }

        return result;
    }
}
